package com.pktechan.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.CustCarriageFlow;
import com.pktechan.manage.entity.SysUser;
import com.pktechan.manage.service.CustCarriageFlowService;
import com.pktechan.manage.service.CustCarriageService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.utils.SessionUtils;
import com.pktechan.manage.vo.CustCarriageFlowVO;
import com.pktechan.manage.vo.CustCarriageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @author: s.zhangbj03307
 * @create: 2021/3/30 13:13
 **/
@Controller
@RequestMapping("custCarriageController")
public class CustCarriageController {

    private static final Logger logger = LoggerFactory.getLogger(CustBaseController.class);

    @Autowired
    private CustCarriageService custCarriageService;
    @Autowired
    private CustCarriageFlowService custCarriageFlowService;

    /**
     * 打开运费管理列表页面
     * @return
     */
    @RequestMapping("toCustCarriageList")
    public String toCustCarriageList() {
        return "custCarriage/custCarriageList";
    }
    /**
     * 打开运费流水列表页面
     * @return
     */
    @RequestMapping("toCustCarriageFlowList")
    public String toCustCarriageFlowList() {
        return "custCarriage/custCarriageFlowList";
    }
    /**
     * 打开运费提现列表页面
     * @return
     */
    @RequestMapping("toCustCarriageWithdrawalList")
    public String toCustCarriageWithdrawalList() {
        return "custCarriage/custCarriageWithdrawalList";
    }

    /**
     * 运费管理列表
     * @param custCarriage
     * @param response
     */
    @RequestMapping(value = "getCustCarriageList",method = RequestMethod.POST)
    public void getCustCarriageList(@RequestBody CustCarriageVO custCarriage, HttpServletResponse response){
        try{
            logger.debug("运费管理列表");
            PageHelper.startPage(custCarriage.getPageNo(), custCarriage.getPageSize());
            List<CustCarriageVO> list = custCarriageService.getList(custCarriage);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerSuccessJson(response,e.getMessage());
        }
    }
    /**
     * 运费流水管理列表/运费提现流水管理列表
     * @param CustCarriageFlow
     * @param response
     */
    @RequestMapping(value = "getCustCarriageFlowList",method = RequestMethod.POST)
    public void getCustCarriageFlowList(@RequestBody CustCarriageFlowVO CustCarriageFlow, HttpServletResponse response){
        try{
            logger.debug("运费流水管理列表/运费提现流水管理列表");
            PageHelper.startPage(CustCarriageFlow.getPageNo(), CustCarriageFlow.getPageSize());
            List<CustCarriageFlowVO> list = custCarriageFlowService.getList(CustCarriageFlow);
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerSuccessJson(response,e.getMessage());
        }
    }
    /**
     * 客户运费提现详情
     *
     * @return
     */
    @RequestMapping("/toCustCarriageWithdrawDetail")
    public String toCustCarriageWithdrawDetail() {
        return "custCarriage/custCarriageWithdrawDetail";
    }

    /**
     * 根据id查询客户运费提现详情
     *
     * @param custCarriageFlow
     * @param response
     */
    @RequestMapping(value = "getCustCarriageWithdrawDetailById", method = RequestMethod.POST)
    public void getCustCarriageWithdrawDetailById(@RequestBody CustCarriageFlow custCarriageFlow, HttpServletResponse response) {
        try {
            logger.info("根据id=" + custCarriageFlow.getId() + "查询客户运费提现详情");
            CustCarriageFlowVO byId = custCarriageFlowService.getById(custCarriageFlow.getId());
            HtmlUtil.writerSuccessJson(response, byId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
    /**
     * 根据id修改运费提现状态
     *
     * @param custCarriageFlow
     * @param response
     */
    @RequestMapping(value = "updateCustCarriageWithdrawStatusById", method = RequestMethod.POST)
    public void updateCustCarriageWithdrawStatusById(@RequestBody CustCarriageFlowVO custCarriageFlow, HttpServletRequest request, HttpServletResponse response) {
        try {
            SysUser user = SessionUtils.getUser(request);
            logger.info("根据id"+custCarriageFlow.getId()+"修改运费提现状态");
            custCarriageFlowService.updateCustCarriageWithdrawStatusById(custCarriageFlow,user.getId());
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
